package com.beimi.net;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import com.beimi.game.service.GameService;
import com.beimi.room.RoomPlayerMgr;
import com.corundumstudio.socketio.SocketIONamespace;
import com.corundumstudio.socketio.SocketIOServer;
import com.next.core.util.AnnotationObjectMap;

@Component
public class ServerRunner implements CommandLineRunner {

	private final SocketIOServer server;

	@Autowired
	private RoomPlayerMgr matchPlayerMgr;
	
	@Autowired
	private GameService gameService;

	@Autowired
	public ServerRunner(SocketIOServer server) {
		this.server = server;
	}

	public void run(String... args) throws Exception {
		SocketIONamespace namespace = server.getNamespace("/bm/game");
		final AnnotationObjectMap<AbstractRoomCmd> map = new AnnotationObjectMap<>();
		map.load("com.beimi.room.cmd");

		namespace.addListeners(new CmdConnect());

		Set<Object> set = map.getInner().keySet();
		for (Object eventName : set) {
			AbstractRoomCmd command = map.get(eventName);
			command.setGameService(gameService);
			command.setMatchPlayerMgr(matchPlayerMgr);

			namespace.addEventListener((String) eventName, String.class, command);
		}

		server.start();
	}
}
